﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc.Uow;
using Volo.Abp.AspNetCore.VirtualFileSystem;
using Volo.Abp;
using Volo.Abp.AspNetCore.VirtualFileSystem;
using Volo.Abp.Auditing;
using Volo.Abp.Modularity;
using Volo.Abp.Security;
using Volo.Abp.Uow;
using Volo.Abp.VirtualFileSystem;
using Microsoft.AspNetCore.Http;
using TianShu.LicenseAuthorizer.Abstracts;

namespace TianShu.WebApi
{
    [DependsOn(
        typeof(AbpAuditingModule),
        typeof(AbpSecurityModule),
        typeof(AbpVirtualFileSystemModule),
        typeof(AbpUnitOfWorkModule),
        typeof(TianShuLicenseAuthorizerAbstractsModule)
        //typeof(AbpHttpModule),
        //typeof(AbpAuthorizationModule),
        //typeof(AbpValidationModule),
        //typeof(AbpExceptionHandlingModule)
        )]
    public class TianShuWebApiModule:AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var services = context.Services;
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            Configure<StaticFileOptions>(options =>
            {
                options.ContentTypeProvider = context.Services.GetRequiredService<AbpFileExtensionContentTypeProvider>();
            });

            Configure<MvcOptions>(mvcOptions =>
            {
                mvcOptions.Filters.AddService(typeof(AbpUowActionFilter));
            });
            context.Services.AddObjectAccessor<IApplicationBuilder>();
        }
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var environment = context.GetEnvironmentOrNull();
            if (environment != null)
            {
                environment.WebRootFileProvider =
                    new CompositeFileProvider(
                        context.GetEnvironment().WebRootFileProvider,
                        context.ServiceProvider.GetRequiredService<IWebContentFileProvider>()
                    );
            }
        }
    }
}
